package com.sunfeng.n6;

import com.sunfeng.TimeUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

/**
 * @author sunfeng
 * @date 2021/02/26 14:26
 **/
@Slf4j(topic = "c.Test2")
public class Test2 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2, new ThreadFactory() {
            private AtomicInteger atomicInteger=new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable runnable) {

                return new Thread(runnable,"MyThread-"+atomicInteger.getAndIncrement());
            }
        });

        ArrayList<Runnable> runnables = new ArrayList<>();

        runnables.add(()->{
            log.debug("1");
        });
        runnables.add(()->{
            log.debug("2");
        });
        runnables.add(()->{
            log.debug("3");
        });
        runnables.add(()->{
            log.debug("4");
        });

        runnables.add(()->{
            log.debug("5");
            TimeUtils.sleep(1);
        });

        runnables.forEach(executorService::execute);
        executorService.shutdown();

    }
}
